--[[								-*- Mode: lua -*-
Baggins_ItemRack

Author: Daphne Pfister <tiiroib02@sneakemail.com>

Thanks to Nargiddley for writing Baggins

License

Copyright (c) 2007 Daphne Pfister

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
]]

-- Feel free to make improvements to this code in SVN including adding
-- locales or merging this into Baggins proper.

Baggins_BagBar = AceLibrary("AceAddon-2.0"):new("AceConsole-2.0","AceHook-2.1")

local Baggins_BagBar = Baggins_BagBar

local dewdrop = AceLibrary:HasInstance("Dewdrop-2.0") and AceLibrary("Dewdrop-2.0")
local L = AceLibrary("AceLocale-2.2"):new("Baggins_BagBar")
local bagframe
local bagbuttons = {}

L:RegisterTranslations("enUs", function()
	return {
	};
end)

-- Local functinos
local dragBagBarStart
local dragBagBarEnd
local enterBagButton -- (button)
local leaveBagButton -- (button)
local toggleBag -- (frame)
local createBagButton -- (id)
local layoutBag -- (id)
local layoutBags

local function dragBagBarStart()
	bagframe:StartMoving()
end

local function dragBagBarStop()
	bagframe:StopMovingOrSizing()
end

local function enterBagButton(button)
	GameTooltip:SetOwner(this, "ANCHOR_LEFT");
	GameTooltip:SetText("Bag Name ...", 1.0, 1.0, 1.0);
end

local function leaveBagButton(button)
	GameTooltip:Hide()
end

local function toggleBag(button)
	local id = button:GetID()
	local bags = Baggins.db.profile.bags
	if (id <= #bags) then
		PlaySound("BAGMENUBUTTONPRESS");
		Baggins:ToggleBag(id)
	end
end
	
local function createBagButton(id)
	local button = bagbuttons[id]
	if not button then
		button = CreateFrame("CheckButton", "Baggins_Bag"..id, bagframe, "ItemButtonTemplate")
		SetItemButtonTexture(button,"Interface\\Buttons\\Button-Backpack-Up")
		button:SetID(id)
		button:SetCheckedTexture("Interface\\Buttons\\CheckButtonHilight")
		bagbuttons[id] = button
		button:SetWidth(36)
		button:SetHeight(36)
		button:ClearAllPoints()
		button:SetMovable(true)
		button:EnableMouse(true)
		if id > 1 then
			button:SetPoint("BOTTOMLEFT",createBagButton(id-1),"BOTTOMRIGHT",0,0)
		else
			button:SetPoint("BOTTOMLEFT",bagframe,"BOTTOMLEFT",0,0)
		end
		button:SetScript("OnClick",toggleBag)
		button:SetScript("OnEnter",enterBagButton)
		button:SetScript("OnLeave",leaveBagButton)
		button:SetScript("OnDragStart",dragBagBarStart)
		button:SetScript("OnDragStop",dragBagBarStop)

		local nametext = button:CreateFontString("Baggins_Bag"..id.."Name","OVERLAY","GameFontNormal")
		button.nametext = nametext
		nametext:SetTextColor(0,0,1,1)
		nametext:SetShadowColor(0,0,0,1)
		nametext:SetShadowOffset(1,1)
		nametext:SetHeight(10)
		nametext:SetPoint("TOP",button,"TOP",0,0) -- Or should it be bottom?
		nametext:Show()
	end
	return button
end

local function layoutBag(id)
	local bag = Baggins.db.profile.bags[id]
	local bagframe = Baggins.bagframes[id]
	if bag and ((not bag.isBank) or Baggins.bankIsOpen) then
		button = createBagButton(id)
		button.nametext:SetText(bag.name)
		button:SetChecked(bagframe and bagframe:IsVisible())
		button:Show()
	else
		button = bagbuttons[id]
		if button then button:Hide() end
	end
end

local function layoutBags()
	local bags = Baggins.db.profile.bags
	local maxid = max(#bags,#bagbuttons)
	for id = 1, maxid do
		layoutBag(id)
	end
	bagframe:SetWidth(36 * #bags)
end

function Baggins_BagBar:OnInitialize()
	bagframe = CreateFrame("Frame","Baggins_BagBarFrame",UIParent)
	bagframe:SetFrameStrata("HIGH")
	bagframe:SetScript("OnDragStart",dragBagBarStart)
	bagframe:SetScript("OnDragStop",dragBagBarStop)
	bagframe:SetPoint("CENTER",0,0)
	bagframe:SetMovable(true)
	bagframe:EnableMouse(true)
	bagframe:SetHeight(36)
end

function Baggins_BagBar:OnEnable()
	layoutBags()
	self:Hook(Baggins,"CloseBag")
	self:Hook(Baggins,"OpenBag")
	self:Hook(Baggins,"MoveBag")
	self:Hook(Baggins,"RemoveBag")
	bagframe:Show()
end

function Baggins_BagBar:OnDisable()
	bagframe:Hide()
end

function Baggins_BagBar:CloseBag(other,id,...)
	self.hooks[other].CloseBag(other,id,...)
	layoutBag(id)	
end

function Baggins_BagBar:OpenBag(other,id,...)
	self.hooks[other].OpenBag(other,id,...)
	layoutBag(id)	
end

function Baggins_BagBar:MoveBag(other,...)
	self.hooks[other].MoveBag(other,...)
	layoutBags()
end

function Baggins_BagBar:RemoveBag(other,...)
	self.hooks[other].MoveBag(other,...)
	layoutBags()
end
